<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-window-widget" :style="getStyle('area')">
      <div class="window-area">
        <div class="replenish" v-if="optionsInfo['replenishShown']">
          <ScrollX>{{ optionsInfo.replenishContent }}</ScrollX>
        </div>
        <div class="window-info flex-box">
          <div class="flex-box-left" :style="getStyle('number')">
            <div class="location" :style="getStyle('room')">
              <ScrollX>
                {{ getRoomInfo }}
              </ScrollX>
            </div>
            <div class="number" :style="getStyle('number')">
              <div class="inside" v-html="optionsInfo['businessContent']"/>
            </div>
          </div>
          <div class="flex-box-right flex-item">
            <div class="publish-area right-item">
              <div class="publish-sign" :style="getStyle('publishSign')" v-if="publishSignShown">
                {{ optionsInfo.publishSign.content || '温馨提示' }}
              </div>
              <div class="publish-container" :key="widgetKey" :class="{'publish-container-hide-sign':!publishSignShown}">
                <template v-if="optionsInfo.publishInfo.tabsName ==='textarea'">
                  <div class="text-area">
                    <ScrollY :content="optionsInfo.publishInfo.textInfo || ''" :pageDuration="10"/>
                  </div>
                </template>
                <template v-else-if="optionsInfo.publishInfo.tabsName ==='video'">
                  <div class="video-wrap">
                    <div class="video-cover-area" ref="videoInfo" v-if="devType">
                      <i class="el-icon-video-play"></i>
                    </div>
                    <video class="video-info-area"
                           preload='auto'
                           controls="controls"
                           autoplay
                           @ended="handleEnded"
                           v-if="!devType"
                           :key="widgetKey"
                    >
                      <source :src="getSource()" type="video/mp4"/>
                      <source :src="getSource()" type="video/ogg"/>
                      很抱歉 , 当前浏览器不支持现有视频格式 ~
                    </video>
                  </div>
                </template>
                <template v-else-if="optionsInfo.publishInfo.tabsName ==='picture'">
                  <div class="picture-area">
                    <img class="logo" :src="getPictureSource()" alt="图片"/>
                  </div>
                </template>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {NoticeBar, Swipe, SwipeItem} from "vant";
import emitter from "@/utils/designer/emitter";
import {getQueryString, pxToVw} from '@/utils/common';
import ScrollY from '@c/public/seamlessScroll/ScrollY.vue';
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {buildParentApi} from "@/utils/designer/util";
import vueSeamlessScroll from 'vue-seamless-scroll';
import {setWindowDeptInfo} from "@/store/modules/actions";
import {v4 as uuid} from "uuid";

export default {
  name: "scene-window-publish-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollX,
    CompOperate,
    NoticeBar,
    Swipe,
    SwipeItem,
    ScrollY,
    vueSeamlessScroll
  },

  props: {
    widget: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      number: 0,
      widgetKey: uuid(),
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg"),
      pause: require("@a/images/design/pause.png"),
    }
  },

  computed: {
    //生成唯一的字符串
    getOnlyId() {
      return function () {
        return uuid()
      }
    },

    // 模板配置行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'area':
            Reflect.set(Obj, 'height', pxToVw(info.areaHeight));
            return Obj;
          case 'room':
            Reflect.set(Obj, 'fontSize', pxToVw(info.roomNameFontSize));
            return Obj;
          case 'number':
            Reflect.set(Obj, 'fontSize', pxToVw(info.roomNumberFontSize));
            return Obj;
          case 'currentTitle':
            Reflect.set(Obj, 'fontSize', pxToVw(info.currentPatientTitleFontSize));
            Reflect.set(Obj, 'color', (info.currentPatientTitleColor));
            Reflect.set(Obj, 'fontWeight', (info.currentPatientTitleFontWeight));
            return Obj;
          case 'currentContent':
            Reflect.set(Obj, 'fontSize', pxToVw(info.currentPatientConnectFontSize));
            Reflect.set(Obj, 'color', (info.currentPatientConnectColor));
            Reflect.set(Obj, 'fontWeight', (info.currentPatientConnectFontWeight));
            return Obj;
          case 'publishSign':
            Reflect.set(Obj, 'fontSize', pxToVw(info.publishSign.fontSize));
            Reflect.set(Obj, 'color', (info.publishSign.fontColor));
            Reflect.set(Obj, 'fontWeight', (info.publishSign.fontWeight));
            return Obj;
        }
      }
    },

    //信息发布标题控制
    publishSignShown(){
      let {publishSign} = this.optionsInfo;
      if(Reflect.has(publishSign,'shown')){
        return publishSign['shown']
      }else {
        return true
      }
    },

    // 获取科室信息
    getWindowDeptInfo() {
      let devType = (this.devType);
      let roomDoctorInfo = this.$store.getters['getWindowDeptInfoInfo'];
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
    },

    //获取医生-患者信息
    getRoomDoctorInfo() {
      return function (type) {
        let temp = Array();
        let devType = (this.devType);
        let roomDoctorInfo = this.$store.getters['getRoomDoctorInfo'];
        let data;
        if (devType) {
          data = roomDoctorInfo.apiData || Array()
        } else {
          data = roomDoctorInfo.mockData || Array()
        }

        if (data?.length) {
          // 截取第一位医生
          let topPlace = data.slice(0, 1);
          // 获取首位医生的就诊和候诊的患者--数组
          return topPlace[0][type] || Array();
        } else {
          temp.push({})
          return temp[0][type] || Array();
        }
      }
    },

    getRoomInfo() {
      const self = this;
      let info = (self.optionsInfo) || Array();
      let deptInfo = (this.getWindowDeptInfo);
      if (info['roomNameAuto']) {
        let type = info['roomNameKey']
        switch (type) {
          case 'roomName':
            return deptInfo['treatmentRoomName'] || deptInfo['roomName'];
          case 'roomShortName':
            return deptInfo['treatmentRoomShortName'] || deptInfo['roomShortName'];
          case 'deptName':
          case 'deptShortName':
            return deptInfo[info['roomNameKey']] || deptInfo['deptName'];
          case 'roomNo':
            return deptInfo['roomNo'] || deptInfo['treatmentRoomNumber'];
          default:
            return deptInfo['deptName']
        }
      } else {
        return info['roomName']
      }
    },

    // 获取视频资源
    getSource() {
      return function () {
        let {publishInfo} = this.optionsInfo;
        let video = publishInfo.videoInfo || []
        if (video && video.length) {
          return `${video[this.number].url}?t=${this.getOnlyId()}&info=${video[this.number].url}`;
        }
      }
    },

    // 获取图片资源
    getPictureSource() {
      return function () {
        let {publishInfo} = this.optionsInfo;
        let pictureInfo = publishInfo.pictureInfo || [];
        if (pictureInfo && pictureInfo.length) {
          return `${pictureInfo[0].url}?t=${this.getOnlyId()}&info=${pictureInfo[0].url}`;
        }
      }
    },

  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    handleEnded(event) {
      let {publishInfo} = this.optionsInfo;
      let videoInfo = event.target.currentSrc;
      let info = getQueryString('info', videoInfo);
      const findIndex = this.$lodash.findIndex((publishInfo.videoInfo), {url: info});
      if (!this.$lodash.isUndefined(findIndex) && !this.$lodash.isNull(findIndex)) {
        let next = (findIndex + 1)
        if (publishInfo.videoInfo.length > next) {
          this.number = next
        } else {
          this.number = 0
        }
      } else {
        this.number = 0
      }
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDoctorInfo":
          console.log(`医生-患者-信息-${data}`);
          this.$store.dispatch('setRoomDoctorInfo', this.$lodash.cloneDeep(data))
          break
        case "roomDeptInfo":
          console.log(`科室诊室信息-${data}`)
          this.$store.dispatch('setWindowDeptInfo', this.$lodash.cloneDeep(data))
          break
          //   关于视频播放
        case "getVideoMedias":
          let element = (this.$refs.videoInfo);
          if (element) {
            let params = Object();
            let info = (this.optionsInfo) || Array();
            const rect = element.getBoundingClientRect();
            const positionInfo = {
              width: Math.ceil(rect.width),
              height: Math.ceil(rect.height),
              top: parseInt(rect.top),
              right: parseInt(rect.right),
              bottom: parseInt(rect.bottom),
              left: parseInt(rect.left),
            };
            Reflect.set(params, 'positionInfo', positionInfo);
            Reflect.set(params, 'video', info['publishInfo']['videoInfo']);
            this.parentApi.executeParentMethod('playVideoMedias', params);
          }
          break;
      }
    },
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = (val.options.config);
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-window-widget {
  color: #333;
  height: 150px;
  font-size: 40px;
  padding: 28px 30px;
  box-sizing: border-box;
  flex: inherit;
  flex-direction: column;
  justify-content: space-between;
  background: #E5EAEF;;
  overflow: hidden;

  .window-area {
    height: 100%;
    display: flex;
    flex-direction: column;
    background: transparent;

    .replenish {
      width: 100%;
      font-size: 80px;
      color: #0059B3;
      line-height: 112px;
      text-align: center;
      font-style: normal;
      @include fontWeight()
    }

    .window-info {
      height: calc(100%);

      .flex-item {
        overflow: hidden;
        color: #FFFFFF;
        height: 100%;
        display: flex;
        text-align: center;
        align-items: center;
        justify-content: center;
        border: none;
      }

      .flex-box-left {
        flex-basis: 50%;
        margin-left: 16px;
        display: flex;
        flex-direction: column;
        border-radius: 20px;
        box-sizing: border-box;
        overflow: hidden;
        color: white;
        background: rgba(0, 89, 179, 1);
        box-shadow: rgba(50, 50, 93, 0.25) 0 50px 100px -20px, rgba(0, 0, 0, 0.3) 0px 30px 60px -30px;

        .location {
          display: flex;
          width: 100%;
          height: 298px;
          min-height: 298px;
          max-height: 298px;
          flex-basis: 298px;
          font-size: 150px;
          align-items: center;
          align-self: center;
          justify-content: center;
          overflow: hidden;
          padding: 0 28px;
          box-sizing: border-box;
          background: rgba(0, 0, 0, 0.1);
        }

        .number {
          width: 100%;
          height: calc(100% - 298px);
          text-align: center;
          background: rgba(0, 89, 179, 0.9);
          font-size: 500px;
          font-weight: bolder;
          display: flex;
          align-items: center;
          align-self: center;
          justify-content: center;

          .inside {
            width: 100%;
            align-self: center;
            color: black;

            p {
              font-size: 150px;
              color: #000000;
              line-height: 170px;
              text-align: center;
              font-style: normal;
            }
          }
        }
      }

      .flex-box-right {
        flex-basis: 50%;
        margin-left: 16px;
        display: flex;
        flex-direction: column;
        border-radius: 20px;
        box-sizing: border-box;
        overflow: hidden;
        box-shadow: 30px 30px 30px -10px rgba(0, 0, 0, 0.15),
        inset 15px 15px 10px rgba(255, 255, 255, 0.75),
        inset -1px -1px 10px rgba(0, 0, 0, 0.2);

        .publish-area {
          width: 100%;
          height: 100%;
          overflow: hidden;
          border-radius: 20px;
          background: white;
          display: flex;
          flex-direction: column;
          border: 1px rgba(0, 0, 0, 0.2) solid;
          box-shadow: 30px 30px 30px -10px rgba(0, 0, 0, 0.15),
          inset 15px 15px 10px rgba(255, 255, 255, 0.75),
          -15px -15px 35px rgba(255, 255, 255, 0.55),
          inset -1px -1px 10px rgba(0, 0, 0, 0.2);

          :deep(.publish-sign) {
            width: 100%;
            height: 100px;
            box-sizing: border-box;
            background: linear-gradient(180deg, #FFFFFF 0%, #F8F8F8 100%);
            border-bottom: 1px solid #DDDDDD;
            font-size: 64px;
            color: #0059B3;
            display: flex;
            justify-content: center;
            align-self: center;
            align-content: center;
            align-items: center;
            overflow: hidden;
            white-space: nowrap;
          }

          :deep(.publish-container) {
            width: 100%;
            height: calc(100% - 100px);
            box-sizing: border-box;

            .text-area {
              padding: 24px 32px;
              height: 100%;
              width: 100%;
              box-sizing: border-box;
              overflow: hidden;
              color: #333333;
              text-align: left;
            }

            .video-wrap {
              height: 100%;
              width: 100%;
              overflow: hidden;
              position: relative;

              .video-cover-area {
                width: 100%;
                height: 100%;
                min-width: 100%;
                overflow: hidden;
                border-radius: 8px;
                display: flex;
                justify-content: center;
                align-items: center;
                background: rgba(0, 0, 0, 0.8);

                i {
                  font-size: 150px;
                  color: #EBEEF5;
                }
              }

              .video-info-area {
                position: absolute;
                inset: 0;
                overflow: hidden;
                border-radius: 8px;
                width: 100%;
                height: 100%;
                object-fit: contain;
              }
            }

            .picture-area {
              width: 100%;
              height: 100%;
              overflow: hidden;

              img {
                width: 100%;
                height: 100%;
                object-fit: contain;
              }
            }
          }
          :deep(.publish-container-hide-sign){
            height: calc(100%);
          }
        }
      }
    }
  }
}
</style>